﻿using System;
using System.Diagnostics.Contracts;
using System.IO;

namespace O1.Kernel.IO
{
    [ContractVerification(true)]
    internal class InputStreamAdapter : InputAdapter
    {
        private readonly Stream stream;

        private readonly bool disposeStream;

        public InputStreamAdapter(Stream stream, bool disposeStream)
        {
            Contract.Requires<ArgumentNullException>(stream != null);

            this.stream = stream;
            this.disposeStream = disposeStream;
        }

        protected override int ReadToBuffer(byte[] buffer, int offset, int count)
        {
            Contract.Assume(buffer != null);
            Contract.Assume(offset >= 0);
            Contract.Assume(count >= 0);
            Contract.Assume(count <= buffer.Length - offset);

            return this.stream.Read(buffer, offset, count);
        }

        [ContractInvariantMethod]
        private void ObjectInvariant()
        {
            Contract.Invariant(this.stream != null);
        }
    }
}
